package XML与JSON.使用SAX;

import java.io.File;
import java.io.IOException;
import java.nio.file.Paths;
import java.util.List;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
//import java.util.logging.Level;
//import java.util.logging.Logger;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import org.xml.sax.SAXException;

//import ch.qos.logback.classic.Level;

public class MyRunner {

    private SAXParser createSaxParser() {

        SAXParser saxParser = null;

        try {

            SAXParserFactory factory = SAXParserFactory.newInstance();
            saxParser = factory.newSAXParser();

            return saxParser;
        } catch (ParserConfigurationException | SAXException ex) {
      	  Logger lgr = LoggerFactory.getLogger(MyRunner.class.getName());
            lgr.debug(ex.getMessage(),ex);
        }

        return saxParser;
    }

    public List<User> parseUsers() {

        MyHandler handler = new MyHandler();
        String fileName = "src\\main\\java\\XML与JSON\\使用SAX\\users.xml";
        //src\\main\\java\\XML与JSON\\使用DOM\\users1.xml
        File xmlDocument = Paths.get(fileName).toFile();

        try {

            SAXParser parser = createSaxParser();
            parser.parse(xmlDocument, handler);

        } catch (SAXException | IOException ex) {
      	  Logger lgr = LoggerFactory.getLogger(MyRunner.class.getName());
           lgr.debug(ex.getMessage(),ex);
        }

        return handler.getUsers();
    }
}

